unit U_ValidaSintegra;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Db, DBClient, StdCtrls, Mask, ToolEdit, Grids, Outline, DirOutln, Buttons,
  DBGrids, ExtCtrls, variants;

type
  TfrmValidaSintegra = class(TForm)
    CDSNota: TClientDataSet;
    CDSItem: TClientDataSet;
    dtNota: TDataSource;
    dtItem: TDataSource;
    CDSNotaCPF_CNPJ: TStringField;
    CDSNotaCodEnt: TStringField;
    CDSNotaNomeEnt: TStringField;
    CDSNotaIE: TStringField;
    CDSNotaDataFat: TDateField;
    CDSNotaUF: TStringField;
    CDSNotaSerie: TStringField;
    CDSNotaNota: TStringField;
    CDSNotaCFOP: TStringField;
    CDSNotaBaseCalc: TFloatField;
    GroupBox1: TGroupBox;
    BitBtn1: TBitBtn;
    edArq: TFilenameEdit;
    Label1: TLabel;
    Panel1: TPanel;
    DBGrid1: TDBGrid;
    Panel2: TPanel;
    DBGrid2: TDBGrid;
    CDSItemCPF_CNPJ: TStringField;
    CDSItemCodEnt: TStringField;
    CDSItemNomeEnt: TStringField;
    CDSItemSerie: TStringField;
    CDSItemCFOP: TStringField;
    CDSItemItem: TStringField;
    CDSItemProduto: TStringField;
    CDSItemQtde: TFloatField;
    CDSItemVlrProd: TFloatField;
    CDSItemNota: TStringField;
    Panel3: TPanel;
    BitBtn2: TBitBtn;
    BitBtn3: TBitBtn;
    GroupBox2: TGroupBox;
    edNum: TEdit;
    Label2: TLabel;
    CDSNotaCancelada: TStringField;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormActivate(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure BitBtn3Click(Sender: TObject);
    procedure DBGrid1CellClick(Column: TColumn);
    procedure edNumKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure DBGrid2DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmValidaSintegra: TfrmValidaSintegra;
  Arq: TextFile;
  texto: string;

implementation

uses clsEntidade, clsBackupVenda, clsNotaVenda, Biblio;

var
  Entidade: TEntidade;

{$R *.DFM}

procedure TfrmValidaSintegra.FormCreate(Sender: TObject);
begin
  Entidade := TEntidade.Create;
end;

procedure TfrmValidaSintegra.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Entidade.Free;
end;

procedure TfrmValidaSintegra.FormActivate(Sender: TObject);
begin
  edArq.SetFocus;
end;

procedure TfrmValidaSintegra.BitBtn1Click(Sender: TObject);
begin
  CDSNota.Close;
  CDSNota.CreateDataSet;
  CDSNota.Open;
  CDSItem.Close;
  CDSItem.CreateDataSet;
  CDSItem.Open;
  if edArq.Text <> '' then begin
    AssignFile(Arq, edArq.Text);
    Reset(Arq);
    while not Eof(Arq) do begin
      Readln(Arq, texto);
      if copy(texto, 3, 3) <> '000' then
        Entidade.Recuperar_cnpj(copy(texto, 3, 14))
      else
        Entidade.Recuperar_cpf(copy(texto, 6, 11));
      if copy(texto, 1, 2) = '50' then begin
        CDSNota.Append;
        CDSNotaCPF_CNPJ.Value := copy(texto, 3, 14);
        CDSNotaCodEnt.Value := Entidade.Cod_Entidade;
        CDSNotaNomeEnt.Value := Entidade.Ent_Nome;
        CDSNotaIE.Value := copy(texto, 17, 14);
        CDSNotaDataFat.Value := StrToDate(botabarr(copy(texto, 31, 08)));
        CDSNotaUF.Value := copy(texto, 39, 02);
        CDSNotaSerie.Value := copy(texto, 43, 03);
        CDSNotaNota.Value := copy(texto, 46, 06);
        CDSNotaCFOP.Value := copy(texto, 52, 03);
        CDSNotaBaseCalc.Value := Formata_String(copy(texto, 55, 13));
        CDSNotaCancelada.Value := copy(texto, 126, 01);
        CDSNota.Post;
      end; //fim do tipo 50

      if copy(texto, 1, 2) = '54' then begin
        CDSItem.Append;
        CDSItemCPF_CNPJ.Value := copy(texto, 3, 14);
        CDSItemCodEnt.Value := Entidade.Cod_Entidade;
        CDSItemNomeEnt.Value := Entidade.Ent_Nome;
        CDSItemSerie.Value := copy(texto, 19, 03);
        CDSItemNota.Value := copy(texto, 22, 06);
        CDSItemCFOP.Value := copy(texto, 28, 03);
        CDSItemItem.Value := copy(texto, 31, 03);
        CDSItemProduto.Value := copy(texto, 34, 14);
        CDSItemQtde.Value := Formata_String(copy(texto, 48, 13));
        CDSItemVlrProd.Value := Formata_String(copy(texto, 61, 12));
        CDSItem.Post;
      end;
    end; //fim do while
    CDSNota.IndexFieldNames := 'Nota';
    CDSItem.IndexFieldNames := 'Nota';
    //CDSItem.MasterSource   := dtNota;
    //CDSItem.MasterFields   := 'Nota';
    //CDSNota.First;

  end else begin
    ShowMessage('Nenhum arquivo localizado');
  end;
end;

procedure TfrmValidaSintegra.BitBtn2Click(Sender: TObject);
begin
  Close;
end;

procedure TfrmValidaSintegra.BitBtn3Click(Sender: TObject);
begin
  CDSNota.Close;
  CDSItem.Close;
  FormActivate(Sender);
end;

procedure TfrmValidaSintegra.DBGrid1CellClick(Column: TColumn);
begin
  if CDSItem.active then
    CDSItem.Locate('Serie;Nota;CodEnt', VarArrayOf([CDSNotaSerie.Value, CDSNotaNota.Value, CDSNotaCodEnt.Value]), []);
end;

procedure TfrmValidaSintegra.edNumKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if key = 12 then
    if CDSNota.Active then
      if CDSNota.Locate('Nota', StrZero(edNum.Text, 6), []) then
        edNum.Clear;
end;

procedure TfrmValidaSintegra.DBGrid2DrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn;
  State: TGridDrawState);
begin
  if (CDSItemNota.Value = CDSNotaNota.Value) and (CDSItemSerie.Value = CDSNotaSerie.Value) and (CDSItemCodEnt.Value = CDSNotaCodEnt.Value) then begin
    //DBGrid2.Columns[0].Color := $00FFDFBF;
    DBGrid2.Columns[1].Color := $00FFDFBF;
    DBGrid2.Columns[2].Color := $00FFDFBF;
    DBGrid2.Columns[3].Color := $00D7FFFF;
  end else begin
    DBGrid2.Columns[0].Color := clWhite;
    DBGrid2.Columns[1].Color := clWhite;
    DBGrid2.Columns[2].Color := clWhite;
    DBGrid2.Columns[3].Color := clWhite;
  end;

  {if (gdSelected in State) then begin
   DBGrid2.Canvas.Brush.Color := clHighlight;
   Dbgrid2.Canvas.Font.Color  := clHighlightText;
  end;}

  Dbgrid2.Canvas.FillRect(Rect);
  Dbgrid2.DefaultDrawDataCell(Rect, dbgrid2.columns[datacol].field, State);
end;

end.
